<html>
<head>
  <title>Sample Change Password Page</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <script src="https://sdk.amazonaws.com/js/aws-sdk-2.3.7.min.js"></script>
	<script>
  AWS.config.region = '<REGION>';
	AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: '<IDENTITY_POOL_ID>'
  });
  var lambda = new AWS.Lambda();
	</script>
  <h1>Sample Change Password Page</h1>
  <div id="info">
  Change Password
  </div>
  <table>
    <tr>
      <td>Email</td>
      <td><input type="email" id="email" size="20">
    </tr>
    <tr>
      <td>Old Password</td>
      <td><input type="password" id="old-password" size="20">
    </tr>
    <tr>
      <td>New Password</td>
      <td><input type="password" id="new-password" size="20">
    </tr>
    <tr>
      <td>Verify New Password</td>
      <td><input type="password" id="verify-new-password" size="20">
    </tr>
    <tr>
      <td colspan="2">
        <button id="change-button">Change Password</button>
      </td>
    </tr>
		<tr>
      <td colspan="2" class="center">
				<a href="index.html">Back</a>
      </td>
    </tr>
  </table>
  <script>
  var info = document.getElementById('info');
  var email = document.getElementById('email');
  var oldPassword = document.getElementById('old-password');
  var newPassword = document.getElementById('new-password');
  var verifyNewPassword = document.getElementById('verify-new-password');
  var changeButton = document.getElementById('change-button');
  changeButton.addEventListener('click', function() {
    info.innerHTML = 'Change Password...';
    if (email.value == null || email.value == '') {
      info.innerHTML = 'Please specify your email address.';
    } else if (oldPassword.value == null || oldPassword.value == '') {
      info.innerHTML = 'Please specify your current password.';
    } else if (newPassword.value == null || newPassword.value == '') {
      info.innerHTML = 'Please specify a new password.';
    } else if (newPassword.value != verifyNewPassword.value) {
        info.innerHTML = 'The new passwords are <b>different</b>, please check.';
    } else {
      var input = {
        email: email.value,
        password: oldPassword.value
      };
      lambda.invoke({
        FunctionName: 'LambdAuthLogin',
        Payload: JSON.stringify(input)
      }, function(err, data) {
        if (err) console.log(err, err.stack);
        else {
          var output = JSON.parse(data.Payload);
					console.log('identityId: ' + output.identityId);
					console.log('token: ' + output.token);
          if (!output.login) {
            info.innerHTML = '<b>Not</b> logged in';
          } else {
            info.innerHTML = 'Logged in with identityId: ' + output.identityId + '<br>';
						var creds = AWS.config.credentials;
						creds.params.IdentityId = output.identityId;
						creds.params.Logins = {
							'cognito-identity.amazonaws.com': output.token
						};
						creds.expired = true;
		        var input = {
		          email: email.value,
		          oldPassword: oldPassword.value,
		          newPassword: newPassword.value
		        };
						AWS.config.credentials.get(function(err) {
			        if (err) console.log(err, err.stack);
			        else {
					      lambda.invoke({
					        FunctionName: 'LambdAuthChangePassword',
					        Payload: JSON.stringify(input)
					      }, function(err, data) {
					        if (err) console.log(err, err.stack);
					        else {
					          var output = JSON.parse(data.Payload);
					          if (!output.changed) {
					            info.innerHTML = 'Password <b>not</b> changed.';
					          } else {
					            info.innerHTML = 'Password changed.';
										}
									}
								});
							}
						});
        	}
        }
      });
    }
  });
  </script>
</body>
</html>
